import axios from 'axios';
import { getCookies } from './cookie';
// 创建 axios 实例
const service = axios.create({
    baseURL: import.meta.env.VITE_APP_BASE_API,
    timeout: 50000,
    headers: { "Content-Type": "application/json;charset=utf-8" },
});

// 请求拦截器
service.interceptors.request.use(
    (config)=>{
        const token = getCookies('token');
        if (token) {
            config.headers['Authori-zation'] = 'Bearer ' + token;
        }
        return config;
    },
    (error) => {
        // do something with request error
        return Promise.reject(error);
    },
);

// 响应拦截
service.interceptors.response.use(
    (response) => {
        let obj = {};
        obj = response.data;
        let status = response.data ? obj.status : 0;
        const code = status;
        switch (code) {
            case 200:
                return obj;
            default:
                return Promise.reject(obj || { msg: '未知错误' });
        }
        return obj;
    },
    (error) => {
        Message.error(error.msg);
        return Promise.reject(error);
    },
);
export default service;